package com.ssyc.common.mapchart;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.ssyc.common.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by loptop on 2017/6/2.
 */
public class PieChartManager {
    PieChart pieChart;
    private int color;

    public PieChartManager(PieChart mPieChart) {
        this.pieChart = mPieChart;
        initChart();
    }

    public PieChartManager(PieChart mPieChart, Context context) {
        this.pieChart = mPieChart;
        this.color = context.getResources().getColor(R.color.tvblack);
        initChart();
    }

    private void initChart() {

        //数据以百分比进行绘制
        pieChart.setUsePercentValues(true);

        //是否显示描述
        pieChart.getDescription().setEnabled(false);

        //设置距离左上右下的位置
        pieChart.setExtraOffsets(5, 10, 5, 5);

        pieChart.setDragDecelerationFrictionCoef(0.95f);

        //设置中间文字,就是空心里面的文字
//        mPieChart.setCenterText(generateCenterSpannableText());

        //设置是实心还是空心，实心false,空心true
        pieChart.setDrawHoleEnabled(false);
        pieChart.setHoleColor(Color.WHITE);

        //透明圆的颜色
        pieChart.setTransparentCircleColor(Color.WHITE);

        //设置透明度0-255，默认100
        pieChart.setTransparentCircleAlpha(110);

        //半径
        pieChart.setHoleRadius(58f);

        //透明圆的半径
        pieChart.setTransparentCircleRadius(60f);

        //中间是否可以添加文字，当为空心
        pieChart.setDrawCenterText(true);

        //设置初始的旋转角度
        pieChart.setRotationAngle(0);

        // 触摸旋转
        pieChart.setRotationEnabled(false);

        //true为点击高亮显示
        pieChart.setHighlightPerTapEnabled(false);

        Legend legend = pieChart.getLegend();
        legend.setEnabled(false);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);


        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setDrawInside(false);
//        legend.setTextColor(Color.WHITE);
        legend.setTextColor(color);
        legend.setXEntrySpace(7f);
        legend.setYEntrySpace(0f);
        legend.setYOffset(10f);

        pieChart.animateXY(1000, 1000);//设置动画 

        // 输入标签样式
//        pieChart.setEntryLabelColor(Color.WHITE);
        pieChart.setEntryLabelColor(color);
        pieChart.setEntryLabelTextSize(10f);

        //不绘制每项上面的显示的Lable标签
        pieChart.setDrawEntryLabels(false);
    }

    /**
     * 设置饼状图
     *
     * @param name   饼状图分类的名字
     * @param date   数值
     * @param colors 颜色集合
     */
    public void setPieChart(List<String> name, List<Float> date, List<Integer> colors) {
        List<PieEntry> yValue = new ArrayList<>();
        for (int i = 0; i < date.size(); i++) {
            PieEntry entry = new PieEntry(date.get(i), name.get(i));
            yValue.add(entry);
        }

        PieDataSet set = new PieDataSet(yValue, "");
        set.setDrawValues(true);
        set.setValueTextSize(12);
        set.setColors(colors);
        set.setValueTextColor(Color.WHITE);
//        set.setValueTextColor(color);
        PieData data = new PieData(set);
        //设置百分比显示
        DecimalFormat df = new DecimalFormat("#0.0");//设置保留小数的位数
        data.setValueFormatter(new PercentFormatter(df));
        pieChart.setData(data);
        pieChart.invalidate(); // refresh
    }

    /**
     * 设实心饼状图
     *
     * @param name   饼状图分类的名字
     * @param date   数值
     * @param colors 颜色集合
     */
    public void setSolidPieChart(List<String> name, List<Float> date, List<Integer> colors) {

        pieChart.setHoleRadius(0);//实心圆   
        pieChart.setTransparentCircleRadius(0);// 半透明圈  
        pieChart.setDrawCenterText(false);//饼状图中间不可以添加文字  

        List<PieEntry> yValue = new ArrayList<>();
        for (int i = 0; i < date.size(); i++) {
            PieEntry entry = new PieEntry(date.get(i), name.get(i));
            yValue.add(entry);
        }
        PieDataSet set = new PieDataSet(yValue, "");
        set.setDrawValues(true);
        set.setValueTextSize(12);
        set.setColors(colors);

//        set.setValueTextColor(Color.WHITE);
        set.setValueTextColor(color);
        PieData data = new PieData(set);
        pieChart.setData(data);
        pieChart.invalidate(); // refresh
    }

    /**
     * 设置饼状图中间的描述内容
     *
     * @param str
     */
    public void setCenterDescription(String str, int color) {
        pieChart.setCenterText(str);
        pieChart.setCenterTextColor(color);
        pieChart.setCenterTextSize(12);
        pieChart.invalidate();
    }

    /**
     * 设置描述信息
     *
     * @param str
     */
    public void setDescription(String str) {
        Description description = new Description();
        description.setText(str);
        pieChart.setDescription(description);
        pieChart.invalidate();
    }
}
